package com.gsta.cdn.cdr.mapper;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.gsta.cdn.cdr.entity.ServerTypeIpTimes;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Mapper
@Component(value = "serverTypeIpTimesMapper")
public interface ServerTypeIpTimesMapper extends BaseMapper<ServerTypeIpTimes>{

    @Select("select * from serverType_IP_times")
    @Results(
            @Result(property = "tarFileName", column = "tar_file_name")
    )
    public  List<ServerTypeIpTimes> selectServerTypeIpTimesByAll();


    @Select("<script>" +
                "SELECT serverIP, date, " +
                "SUM(CASE WHEN serverType = '16' THEN times ELSE 0 END) vod, " +
                "SUM(CASE WHEN serverType = '224' THEN times ELSE 0 END) live, " +
                "SUM(CASE WHEN serverType = '148' THEN times ELSE 0 END) tvod " +
                "FROM serverType_IP_times " +
                "GROUP BY date, serverIP " +
                "HAVING date = #{date} " +
            "</script>")
    List<Map<String, Object>> selectSum(String date);


    @Select("<script>" +
                "SELECT COUNT(*) FROM ( " +
                "SELECT serverIP, date, " +
                "SUM(CASE WHEN serverType = '16' THEN times ELSE 0 END) vod, " +
                "SUM(CASE WHEN serverType = '224' THEN times ELSE 0 END) live, " +
                "SUM(CASE WHEN serverType = '148' THEN times ELSE 0 END) tvod " +
                "FROM serverType_IP_times " +
                "GROUP BY date, serverIP " +
                "HAVING date = #{date}) a " +
            "</script>")
    int countSum(String date);
}
